package org.taishan.electronshop.servlet;
import org.taishan.electronshop.dao.*;
import org.taishan.electronshop.domain.Customer;
import org.taishan.electronshop.domain.Order;
import org.taishan.electronshop.domain.OrderItem;
import org.taishan.electronshop.domain.TrolleyItem;

import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order/*")
public class OrderServlet extends HttpServlet{
    private TrolleyDao td = new TrolleyDao();
    private GoodsDao gd = new GoodsDao();
    private CustomerDao cd = new CustomerDao();
    private OrderDao od = new OrderDao();
    private OrderItemDao oitd = new OrderItemDao();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        /*换成int报错空指针异常 */
        Integer userid=(Integer)session.getAttribute("userid");
        System.out.println(userid);
        if(username == null || username.length() <=0) {
            Customer c = cd.load(1);
            username = c.getUsername();
            userid = c.getId();
            session.setAttribute("userid", userid);
            session.setAttribute("username", username);
            session.setAttribute("customer", c);
        }else {
            Customer c = (Customer)session.getAttribute("customer");
            userid = c.getId();
            session.setAttribute("userid", userid);
        }
        String uri = request.getRequestURI(); // 从 请求对象中获取 URI
        System.out.println("URI : " + uri);
        /**
         * uri : /employee           ========>  firstSlash == lastSlash
         * uri : /employee/         ========>   lastSlash == length -1
         * uri : /employee/xiaowen
         * uri : /employee/hezi/qing
         */
        int firstSlash = uri.indexOf("/"); // 获得第一个 / 对应的索引
        int lastSlash = uri.lastIndexOf("/"); // 获得最后一个 / 对应的索引
        int length = uri.length(); // 获得 字符串的长度
        int dotIndex = uri.lastIndexOf("."); // 从 uri 中获得最后一个 圆点的 索引

        // 如果最后一个斜杠不是第一个斜杠、并且最后一个斜杠不是最后一个字符、并且没有圆点
        if (firstSlash != lastSlash && lastSlash != length - 1 && dotIndex == -1) {
            String m = uri.substring(lastSlash + 1); // 截取 最后一个斜杠之后的内容
            System.out.println("最后一个 / 之后的内容:  " + m);
            m = m.toLowerCase(); // 全部转小写
            // JDK 1.5  之前 switch 中允许使用 byte 、short 、char、int ，从JDK 1.5 开始支持 枚举
            switch (m) { // Java 7 开始支持在  switch 中使用 String
                case "order-detail":
                    orderDetail(request, response);
                    break;
                case "order-change":
                    orderChange(request, response);
                    break;
                case "order-onelist":
                    orderOnelist(request, response);
                    break;
                case "order-alllist":
                    orderAlllist(request, response);
                    break;
                default:
                    break;
            }
        }
    }
/*收货*/
    private void orderChange(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        String oid = request.getParameter("oid");
        Order o = od.load(username,oid);
        String operate = request.getParameter("operate");
        switch (operate){

            case "付款":{
                o.setState("待收货");
                o.setOperate("收货");
                od.update(o);
                request.removeAttribute("state");
               request.setAttribute("state","待收货");
//                response.sendRedirect("/order/order-onelist?state=待收货");
                RequestDispatcher requestDispatcher= request.getRequestDispatcher("/order/order-onelist?state=待收货");
                requestDispatcher.forward(request,response);
                break;
            }
            case "收货":{
                o.setState("待评价");
                o.setOperate("评价");
                od.update(o);
                request.removeAttribute("state");
                request.setAttribute("state","待评价");
                RequestDispatcher requestDispatcher= request.getRequestDispatcher("/order/order-onelist?state=待评价");
                requestDispatcher.forward(request,response);
                break;
            }
            case "评价":{
                o.setState("已完成");
                o.setOperate(null);
                od.update(o);
                request.removeAttribute("state");
                request.setAttribute("state","已完成");
                RequestDispatcher requestDispatcher= request.getRequestDispatcher("/order/order-onelist?state=已完成");
                requestDispatcher.forward(request,response);
                break;
            }
            case "退款":{
                o.setState("已退款退货");
                o.setOperate(null);
                od.update(o);
                request.removeAttribute("state");
                request.setAttribute("state","已退款退货");
               RequestDispatcher requestDispatcher= request.getRequestDispatcher("/order/order-onelist?state=已退款退货");
               requestDispatcher.forward(request,response);
                break;
            }

        }

        return;

    }
    /*根据状态列出订单*/
    private void orderOnelist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("orders");
        String username = (String) session.getAttribute("username");
        Integer userid = (Integer) session.getAttribute("userid");
        System.out.println(userid);
        String state = request.getParameter("state");
        System.out.println("xi"+state);
        List<Order> orders = new ArrayList<>();
        orders = od.list(userid,state);
        session.setAttribute("orders", orders);

        response.sendRedirect("/order.vm");
        return;
    }


    private void orderAlllist(HttpServletRequest request, HttpServletResponse response) throws IOException {

        HttpSession session = request.getSession();
        session.removeAttribute("orders");
        String username = (String) session.getAttribute("username");
        Integer userid = (Integer) session.getAttribute("userid");
        List<Order> orders = new ArrayList<>();
        orders = od.list(userid);
        session.setAttribute("orders", orders);
        response.sendRedirect("/order.vm");
        return;
    }
 /*详情*/
    private void orderDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("noworder");
        session.removeAttribute("noworderitems");
        Customer c = (Customer)session.getAttribute("customer");
        String username = (String)session.getAttribute("username");

        String oid =request.getParameter("oid");
        System.out.println("oid"+oid);
        Order o = od.load(username,oid);
        System.out.println("o"+o);
        OrderItem oit =oitd.load(oid);

        System.out.println("oit"+oit);
        session.setAttribute("noworder",o);
        session.setAttribute("noworderitems",oit);
        response.sendRedirect("/order-items.vm");
        return;
    }


}

